import Vue from 'vue'
import App from './App.vue'
import router from './router'
import FastClick from 'fastclick'
import { vueBaberrage } from 'vue-baberrage'
import Loading from "./components/loading/index";
import "normalize.css/normalize.css"; // A modern alternative to CSS resets
import "@/styles/index.scss";
import { getOenIdByCode } from "@/api/common";
import { getQueryString } from "@/utils/utils";

Vue.use(vueBaberrage);
Vue.use(Loading);

Vue.config.productionTip = false

router.beforeEach((to, from, next) => {
  const isDevMode = process.env.NODE_ENV === "development";
  if (!isDevMode) {
    const appid = process.env.VUE_APP_APPID;
    const url = "http://www.whxgjt.com/dfh2/";
    const openid = localStorage.getItem("openid");
    if (!openid) {
      location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(url)}&response_type=code&scope=snsapi_base&state=111#wechat_redirect`

      const code = getQueryString("code");
      if (code) {
        getOpenid();
        async function getOpenid() {
          try {
            const res = await getOenIdByCode(code);
            const data = res.data;
            if (data.errorCode == "0") {
              localStorage.setItem("openid", JSON.stringify(data.data));
            } else {
              next();
            }
          } catch (e) {
            console.log(e);
            next();
          }
        }
      } else {

        next()
      }
    } else {
      next()
    }
  } else {
    next();
  }

})


//去除300毫秒的点击延迟
FastClick.prototype.focus = function (targetRlement) {
  'use strict';
  targetRlement.focus();
}
if ('addEventListener' in document) {
  document.addEventListener('DOMContentLoaded', function () {
    FastClick.attach(document.body);
  }, false);
}

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
